package com.at.controller;

import com.at.domain.BlogQuery;
import com.at.domain.Type;
import com.at.service.BlogService;
import com.at.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * @author : code1997
 * @date :2020-10-2020/10/17 11:00
 */
@Controller
public class TypeShowController {

    @Autowired
    private TypeService typeService;
    @Autowired
    private BlogService blogService;

    @GetMapping({"types/{id}"})
    public String helloController2(@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                                   @PathVariable Long id, Model model){
        //相当于查询全表
        List<Type> types=typeService.listTypeTop(10000);
        if (id==-1){
            Type type = types.get(0);
            /*避免没有类型导致出现空指针异常的现象*/
            if (type!=null){
                id=type.getId();
            }
        }
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setTypeId(id);
        model.addAttribute("types", types);
        model.addAttribute("page", blogService.listBlog(pageable,blogQuery));
        model.addAttribute("activeTypeId", id);
        return "types";
    }

}
